<?php

namespace App\Admin\Controllers;

use App\Models\AppRelease;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AppReleaseController extends AdminController
{

    protected $title = '应用更新';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AppRelease(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('d');
            $grid->column('is_default')->display(function($q){
                return $q?'当前版本':'老版本';

            });
            $grid->column('v');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AppRelease(), function (Show $show) {
            $show->field('id');
            $show->field('d');
            $show->field('is_default');
            $show->field('v');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AppRelease(), function (Form $form) {
            $form->display('id');
            $form->text('d');
            $form->select('is_default')->options([0=>'老版本',1=>'当前版本']);
            $form->text('v');
            $form->display('created_at');
            $form->display('updated_at');
            $form->footer(function ($footer) {
                $footer->disableViewCheck();
                $footer->disableReset();

                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();

                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
        });
        });
    }
    public function store()
    {
        $is_default=request('is_default');
        if($is_default){
            AppRelease::query()->update(['is_default'=>0]);
        }
        return $this->form()->store();
    }
}
